﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using TraitorAmongUs.States;

namespace TraitorAmongUs {
	public class StateManager {
		private GameState currentState = null;
		private Engine engine = null;

		bool stateLoaded = false;

		public StateManager(Engine e) {
			engine = e;
		}

		public void SetState(GameState state, object param) {
			stateLoaded = false;

			if(currentState != null) currentState.LeaveState();

			currentState = state;

			if(currentState != null) {
				if(param == null) param = new object();
				currentState.EnterState(engine, param);
				stateLoaded = true;
			} else {
				throw new Exception("State cannot be null");
			}
		}

		public void Update(GameTime gameTime) {
			if(currentState != null && stateLoaded) currentState.Update(gameTime);
		}

		public void Draw(GameTime gameTime) {
			if(currentState != null && stateLoaded) currentState.Draw(gameTime);
		}

		public void Exit() {
			// Called when the program is exiting
			currentState.LeaveState();

			// Shut down the server if it's running
			if(engine.gameServer != null) {
				engine.gameServer.Close();
			}
		}
	}
}
